package com.fire.control.system.config.quartz;
import java.io.IOException;
import java.util.Properties;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;


/**
* @ClassName: QuartzConfig
* @Description: TODO(设置调度工厂)
* @author wanbo
* @date 2017年6月20日 上午1:40:05
*/
@Configuration
public class QuartzConfig {
	
	@Autowired
    private MyJobFactory myJobFactory;
	
    @Value("${spring.datasource.driver-class}")
    private String myDSDriver;
    @Value("${spring.datasource.url}")
    private String myDSURL;
    @Value("${spring.datasource.username}")
    private String myDSUser;
    @Value("${spring.datasource.password}")
    private String myDSPassword;
    @Value("${spring.datasource.maxActive}")
    private String myDSMaxConnections; 
    @Value("${lora.quartz.schedulerName}")
	private String schedulerName;
    
    @Bean
    public Scheduler scheduler() throws IOException, SchedulerException {  
        Scheduler scheduler = schedulerFactoryBean().getScheduler(); 
        scheduler.start();
        return scheduler;
    }
    
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setSchedulerName(schedulerName);
        factory.setWaitForJobsToCompleteOnShutdown(true);//当spring关闭时,会等待所有已经启动的quartz job结束后spring才能完全shutdown
        factory.setOverwriteExistingJobs(true);
        //延时启动
        factory.setStartupDelay(10);
        //加载quartz数据源配置
        factory.setQuartzProperties(quartzProperties());
        //自定义Job Factory,用于Spring注入
        factory.setJobFactory(myJobFactory);
        return factory; 
    }
    
     public Properties quartzProperties() throws IOException {  
     	 Properties prop = new Properties();
     	 //参考http://blog.csdn.net/ye_sheng/article/details/48396161
     	 //prop.put("org.quartz.scheduler.instanceName", quartzInstanceName);
         prop.put("org.quartz.scheduler.instanceId", "AUTO");
         prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
         prop.put("org.quartz.scheduler.jmx.export", "true");
          
         prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
         prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
         prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
         prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
         prop.put("org.quartz.jobStore.isClustered", "true");
          
         prop.put("org.quartz.jobStore.clusterCheckinInterval", "30000");//30秒检查一次
         prop.put("org.quartz.jobStore.dataSource", "myDS");
         prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
         prop.put("org.quartz.jobStore.misfireThreshold", "60000");
         prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "true");
         prop.put("org.quartz.jobStore.selectWithLockSQL", "SELECT * FROM {0}LOCKS WHERE LOCK_NAME = ? FOR UPDATE");
          
         prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
         prop.put("org.quartz.threadPool.threadCount", "10");
         prop.put("org.quartz.threadPool.threadPriority", "5");
         prop.put("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
          
         prop.put("org.quartz.dataSource.myDS.driver", myDSDriver);
         prop.put("org.quartz.dataSource.myDS.URL", myDSURL);
         prop.put("org.quartz.dataSource.myDS.user", myDSUser);
         prop.put("org.quartz.dataSource.myDS.password", myDSPassword);
         prop.put("org.quartz.dataSource.myDS.maxConnections", myDSMaxConnections);
          
         //prop.put("org.quartz.plugin.triggHistory.class", "org.quartz.plugins.history.LoggingJobHistoryPlugin");
         prop.put("org.quartz.plugin.shutdownhook.class", "org.quartz.plugins.management.ShutdownHookPlugin");
         prop.put("org.quartz.plugin.shutdownhook.cleanShutdown", "true");
         return prop;
     }
}